package com.zhengtoon.mctoon.conv.utils.sdzc;

import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.sdzc.Feedback;
import com.zhengtoon.mctoon.conv.bean.sdzc.Letter;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.utils.SysConfig;
import com.zhengtoon.mctoon.conv.wsparam.output.ZfhfOutputResult;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 政风行风系统 工具类
 *
 * @author 144637
 */
public class ZfhfUtils {

    private final static Logger logger = LoggerFactory.getLogger(ZfhfUtils.class);

    private final static String COMMUNITY_CODE = SysConfig.getPropertyValue("config.communityCode");
    private final static String COMMUNITY_UUID = SysConfig.getPropertyValue("config.communityUUID");

    /**
     * 提交信件入参转换 144637
     */
    public static String getSubmitCommunityLetterParam(Letter letter, boolean haveAttach) {
        String argCountString = haveAttach ? "<arguments count=\"16\">" : "<arguments count=\"15\">";
        String attachString = haveAttach ? "<argument index=\"16\" name=\"letterAttachZipStream\" data_type=\"string\">" + letter.getLetterAttachZipStream() + "</argument>" : "";
        // TODO  参数校验
        String xmlParam =
                "<argument_list>" +
                        argCountString +
                        "<argument index=\"1\" name=\"communityCode\" data_type=\"string\">" + COMMUNITY_CODE + "</argument>" +
                        "<argument index=\"2\" name=\"communityUUID\" data_type=\"string\">" + COMMUNITY_UUID + "</argument>" +
                        "<argument index=\"3\" name=\"sysLetterID\" data_type=\"string\">" + letter.getSysLetterId() + "</argument>" +
                        "<argument index=\"4\" name=\"letterType\" data_type=\"string\">" + letter.getLetterType() + "</argument>" +
                        "<argument index=\"5\" name=\"letterTitle\" data_type=\"string\">" + letter.getLetterTitle() + "</argument>" +
                        "<argument index=\"6\" name=\"letterContent\" data_type=\"string\">" + letter.getLetterContent() + "</argument>" +
                        "<argument index=\"7\" name=\"isOpen\" data_type=\"string\">" + letter.getOpenType() + "</argument>" +
                        "<argument index=\"8\" name=\"remindType\" data_type=\"string\">" + letter.getRemindType() + "</argument>" +
                        "<argument index=\"9\" name=\"writer\" data_type=\"string\">" + letter.getWriter() + "</argument>" +
                        "<argument index=\"10\" name=\"writerName\" data_type=\"string\">" + letter.getWriterName() + "</argument>" +
                        "<argument index=\"11\" name=\"writerEmail\" data_type=\"string\">" + letter.getWriterEmail() + "</argument>" +
                        "<argument index=\"12\" name=\"writerMobile\" data_type=\"string\">" + letter.getWriterMobile() + "</argument>" +
                        "<argument index=\"13\" name=\"writerTelephone\" data_type=\"string\">" + letter.getWriterTelephone() + "</argument>" +
                        "<argument index=\"14\" name=\"writerPostcode\" data_type=\"string\">" + letter.getWriterPostCode() + "</argument>" +
                        "<argument index=\"15\" name=\"writerAddress\" data_type=\"string\">" + letter.getWriterAddress() + "</argument>" +
                        attachString +
                        "</arguments>" +
                        "</argument_list>";
        return xmlParam;
    }

    /**
     * 获取信件列表入参转换   144637
     */
    public static String getCommunityLetterListParam() {
        String xmlParam =
                "<argument_list>" +
                        "<arguments count=\"2\">" +
                        "<argument index=\"1\" name=\"communityCode\" data_type=\"string\">" + COMMUNITY_CODE + "</argument>" +
                        "<argument index=\"2\" name=\"communityUUID\" data_type=\"string\">" + COMMUNITY_UUID + "</argument>" +
                        "</arguments>" +
                        "</argument_list>";
        return xmlParam;
    }

    /**
     * 获取信件回复结果入参转换 144637
     *
     * @param letterId 中科软系统信件id
     */
    public static String getCommunityLetterReplyParam(String letterId) {
        String xmlParam =
                "<argument_list>" +
                        "<arguments count=\"3\">" +
                        "<argument index=\"1\" name=\"communityCode\" data_type=\"string\">" + COMMUNITY_CODE + "</argument>" +
                        "<argument index=\"2\" name=\"communityUUID\" data_type=\"string\">" + COMMUNITY_UUID + "</argument>" +
                        "<argument index=\"3\" name=\"letterID\" data_type=\"string\">" + letterId + "</argument>" +
                        "</arguments>" +
                        "</argument_list>";
        return xmlParam;
    }

    /**
     * 获取信件办理结果附件入参转换   144637
     */
    public static String getCommunityLetterReplyAttachmentParam(String replyId, String attachId) {
        String xmlParam =
                "<argument_list>" +
                        "<arguments count=\"4\">" +
                        "<argument index=\"1\" name=\"communityCode\" data_type=\"string\">" + COMMUNITY_CODE + "</argument>" +
                        "<argument index=\"2\" name=\"communityUUID\" data_type=\"string\">" + COMMUNITY_UUID + "</argument>" +
                        "<argument index=\"3\" name=\"replyID\" data_type=\"string\">" + replyId + "</argument>" +
                        "<argument index=\"4\" name=\"attachID\" data_type=\"string\">" + attachId + "</argument>" +
                        "</arguments>" +
                        "</argument_list>";
        return xmlParam;
    }


    /**
     * 提交评价入参转换 144637
     */
    public static String getSubmitReplyParam(Feedback feedback) {
        String xmlParam =
                "<argument_list>" +
                        "<arguments count=\"5\">" +
                        "<argument index=\"1\" name=\"communityCode\" data_type=\"string\">" + COMMUNITY_CODE + "</argument>" +
                        "<argument index=\"2\" name=\"communityUUID\" data_type=\"string\">" + COMMUNITY_UUID + "</argument>" +
                        "<argument index=\"3\" name=\"transferLetterID\" data_type=\"string\">" + feedback.getTransferLetterId() + "</argument>" +
                        "<argument index=\"4\" name=\"satisfyType\" data_type=\"string\">" + feedback.getSatisfyType() + "</argument>" +
                        "<argument index=\"5\" name=\"reason\" data_type=\"string\">" + feedback.getReason() + "</argument>" +
                        "</arguments>" +
                        "</argument_list>";
        return xmlParam;
    }

    /**
     * 提价评价出参转换
     */

    public static ZfhfOutputResult parseSubmitReplyResult(String xmlText) {

        ZfhfOutputResult result = new ZfhfOutputResult();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlText);
            Element root = doc.getRootElement();
            result.setResult_type(root.element("result_type").getText());
            result.setResult_status(root.element("result_status").getText());

            Element data = root.element("user_define_information");
            result.setOperate_result(data.element("operate_result").getText());

        } catch (Exception e) {
            logger.error("解析xml失败", e);
            throw new BusinessException(ConvPhotoExceptionCode.PARSE_XML_FAIL);
        }
        return result;

    }

}
